#pragma once

namespace cybertron {

#define PACKET_MAX_SIZE 2048
#define PACKET_HEAD_LENGTH 4
#define PACKET_MEMORY_PREALLOCATE
/* 
 * Head: two bytes in big endian to specify the length of the body
*/
class NetPacket {
public:
	NetPacket();
	~NetPacket();

	const void *body() { return (const void *)_body; };
	const size_t body_len() { return _body_len; };

	int set_data(const void *data, size_t len);
	size_t appand_data(const void *data, size_t len);

	bool all_recv();
	char *recv_buf();
	size_t recv_buf_len();
	int commit_recv(size_t len);

	bool all_sent();
	const char *send_buf();
	size_t send_buf_len();
	void commit_send(size_t len);
private:
#ifdef PACKET_MEMORY_PREALLOCATE
	uint8_t *_head;
#else
	uint8_t _head[PACKET_HEAD_LENGTH];
#endif
	size_t	_head_pos;
	uint8_t *_body;
	size_t _body_len;
	size_t _body_pos;
};
}
